#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os, subprocess, sys
import PyQt5.uic
from PyQt5 import QtCore, QtWidgets,Qt
from importlib import reload
from launch_app import set_buttons

file_path = str(os.path.split(os.path.realpath(__file__))[0])

def compile_qrc():
    qrc_path = os.path.join(file_path, 'qrc')
    sys.path.append(qrc_path)
    import_grp = list()
    for qrc in os.listdir(qrc_path):
        if qrc.endswith('.qrc'):
            py_name = qrc_path + "/" + qrc.split('.')[0] + "_rc.py"
            if not os.path.isfile(py_name):
                import_name = qrc.split('.')[0]+'_rc'
                qrc_name =  qrc_path + "/" + qrc
                subprocess.Popen('pyrcc5 -o {0} {1}'.format(py_name,qrc_name))
                import_grp.append(import_name)
    return import_grp

compile_qrc()
from GUI import qrc
reload(qrc)
MainWindowForm, MainWindowBase = PyQt5.uic.loadUiType(file_path + '/main.ui')

class MainWindow(MainWindowBase, MainWindowForm):
    def __init__(self, parent = None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
        self.Magic_logo_btn.clicked.connect(self.read_web)
        self.arrange_launch_btns()
        # self.setAttribute(Qt.Qt.WA_NoSystemBackground, True)
        self.setAttribute(Qt.Qt.WA_TranslucentBackground, True)
        self.setWindowFlags(Qt.Qt.FramelessWindowHint)

    def mousePressEvent(self, event):
        if event.button() == Qt.Qt.LeftButton:
            self.m_flag = True
            self.m_Position = event.globalPos() - self.pos()  # 获取鼠标相对窗口的位置
            event.accept()
            # self.setCursor(Qt.QCursor(Qt.Qt.OpenHandCursor))  # 更改鼠标图标

    def mouseMoveEvent(self, QMouseEvent):
        if Qt.Qt.LeftButton and self.m_flag:
            self.move(QMouseEvent.globalPos() - self.m_Position)  # 更改窗口位置
            QMouseEvent.accept()

    def mouseReleaseEvent(self, QMouseEvent):
        self.m_flag = False
        self.setCursor(Qt.QCursor(Qt.Qt.ArrowCursor))

    def read_web(self):
        import webbrowser
        url = 'http://www.octmedia.com/'
        webbrowser.open(url, new=0, autoraise=True)

    def arrange_launch_btns(self):
        set_buttons.add_launch_buttons(self.maya_btn_grp)
        return True

def main():
    app = None
    if not app:
        app = QtWidgets.QApplication([])
    window = MainWindow()
    window.move(1500, 200)
    # window.setAutoFillBackground(True)
    window.raise_()
    window.show()
    if app:
        app.exec_()


if __name__ == '__main__':
    main()
